<template>
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="dialogTableVisible">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="用户角色" prop="roleName">
                <el-input v-model="ruleForm.roleName"></el-input>
            </el-form-item>

            <el-form-item label="状态" prop="status">
                <el-radio-group v-model="ruleForm.status">
                    <el-radio label="0">正常</el-radio>
                    <el-radio label="1">禁用</el-radio>
                </el-radio-group>
            </el-form-item>
            
            <el-form-item label="备注" prop="remark">
                <el-input v-model="ruleForm.remark"></el-input>
            </el-form-item>

            <el-form-item label="排序" prop="roleSort">
                <el-input v-model="ruleForm.roleSort"></el-input>
            </el-form-item>

            <el-form-item style="text-align: center;">
                <el-button type="primary" @click="submitForm('ruleForm')">{{title=='新增'?'立即创建':'修改'}}</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
            </el-form>
    </el-dialog>
</template>


<script> 
 import { saveRole } from '@/api/home_'

export default {
  data(){
      return{
        title:'新增',
        dialogTableVisible:false,
        ruleForm: {
            "createTime": "",
            "createUserid": 0,
            "remark": "",
            "roleId": 0,
            "roleKey": "",
            "roleName":"",
            "updateUserid":0,
            "roleSort": 1,
            "status": '0',
            "updateTime": ""
        },
        rules: {
            roleName: [
                { required: true, message: '请输入角色名称', trigger: 'blur' }
            ],
        }
      }
  },
  filters:{
      
  },
  components: {
 
  },
  created(){
     
  },
  methods: {
    submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            saveRole(this.ruleForm).then(res=>{
                if(res.code==200){
                    this.$message.success(res.msg)
                    this.$emit('refresh')
                    this.dialogTableVisible = false
                }else{
                    this.$message.error(res.msg)
                }
            })
          } else {
            return false;
          }
        });
      },
      resetForm() {
        this.ruleForm={
            "createTime": "",
            "createUserid": 0,
            "remark": "",
            "roleId": 0,
            "roleKey": "",
            "roleName":"",
            "updateUserid":0,
            "roleSort": 1,
            "status": '0',
            "updateTime": ""
        }
      },
    setVal(v){
        this.title = v=='add'?'新增':'修改'
        if(v!='add'){
           this.ruleForm = JSON.parse(v)
           this.ruleForm.sex = this.ruleForm.sex+''
           this.ruleForm.status = this.ruleForm.status+''
        }else{
            this.resetForm()
        }
        this.dialogTableVisible = true
    }
  }
}
</script>

<style lang="scss" scoped>
.el-form{
    display:flex;
    flex-direction:row;
    flex-wrap:wrap;
    .el-form-item{
        width:50%;
    }
}
</style>
